package edu.must.uh.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class BeanUtils {
    
    public  static <T> void transListMap2BeanList(List<Map<String, Object>> maplist, List<T> obj,Class<T> oclass) throws Exception {
        
        if(obj ==null) {
            obj = new ArrayList<T>();
        }  
        if(maplist !=null && !maplist.isEmpty()) {
            for (int i = 0; i < maplist.size(); i++) {
               Map<String, Object> map =maplist.get(i);   
               String jsonString = JSON.toJSONString(map);
               T beanT = (T) JSONArray.parseObject(jsonString, oclass);             
                obj.add(beanT);
            }
        }      
   }
    public static void transRequest2Bean(HttpServletRequest request, Object obj) throws Exception {  
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String fieldName = property.getName();
                String key = fieldName.toUpperCase();                
                Enumeration enu=request.getParameterNames(); 
                while(enu.hasMoreElements()){ 
                  String paraName=(String)enu.nextElement(); 
                  String paraName2 = paraName.toUpperCase();
                  if(paraName2.equals(key)) {
                      Method setter = property.getWriteMethod();
                      Object value =ToolsOfString.conveString(request.getParameter(paraName)) ;       
                      if(property.getPropertyType() == java.util.Date.class) { 
                          if(Tools.notEmpty(value.toString())) {
                              Date sDate = null;
                             if(Tools.checkDate(value.toString())) {
                                 sDate =  Tools.str2Date((String) value);
                             }else {
                                 sDate = new Date((long) value);  
                             }                              
                             setter.invoke(obj, sDate);  
                          }                                           
                    }else if(property.getPropertyType() == java.lang.Integer.class) {  
                        if(Tools.isEmpty(value.toString())) {
                            value = 0;
                        }
                        Integer ival  = Integer.parseInt(value.toString()) ;
                        setter.invoke(obj, ival);
                    }else{
                        setter.invoke(obj, value);  
                    }
                    break;
                  }   
                }
            }         
    }
    

    // Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean
    public static void transMap2Bean(Map<String, Object> map, Object obj) throws Exception {  
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String fieldName = property.getName();
                String key = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    // 得到property对应的setter方法
                    Method setter = property.getWriteMethod();
                   if( property.getPropertyType() == java.util.Date.class) {  
                       Date sDate = new Date((long) value); 
                       setter.invoke(obj, sDate);
                   }else {
                       setter.invoke(obj, value);  
                   }
                }

            }
         
    }
    
  //把JsonArray的字符串转换成List<Map<String, Object>>
    public static List<Map<String, Object>> parseJsonArrayStrToListForMaps(String jsonArrayStr) {
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        try {
            if(jsonArrayStr != null) {
                JSONArray jsonArray = JSONArray.parseArray(jsonArrayStr);
                Map<String, Object> map = null;
                for(int j=0;j<jsonArray.size();j++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(j);
                    map = parseJsonObjectStrToMap(jsonObject.toString());
                    if(map != null) {
                        list.add(map);
                    }
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        if(list.size() == 0) {
            return null;
        }
        return list;
    }
    
  //把JsonObject的字符串转换成Map<String, Object>
    public static Map<String, Object> parseJsonObjectStrToMap(String jsonObjectStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            if(jsonObjectStr != null) {
                JSONObject jsonObject =JSON.parseObject(jsonObjectStr);
                Iterator iter = jsonObject.entrySet().iterator();
                while(iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    String key = entry.getKey().toString();
                    Object value = entry.getValue();
                    map.put(key, value);
                }
              
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        if(map.size() == 0) {
            return null;
        }
        return map;
    }


    // Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map
    public static Map<String, Object> transBean2Map(Object obj) {

        if(obj == null){
            return null;
        }        
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);

                    map.put(key, value);
                }

            }
        } catch (Exception e) {
            System.out.println("transBean2Map Error " + e);
        }

        return map;

    }

}
